package smlab.effects
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 
	 * @author simon
	 * original author:pig-hong
	 * 
	 */
	public class SnowController
	{
		private static var _snowA:Array;
		private static var _timer:Timer;
		private static var _parant:DisplayObjectContainer;
		private static var _snowShowDelay:uint;
		private static var _snowXRange:Number;
		private static var _snowXAngle:Number;
		private static var _baseYSpeed:Number;
		private static var _scaleMin:Number;
		private static var _scaleMix:Number;
		
		/**
		 * @param parant		雪花的容器
		 * @param snowShowDelay	每产生一朵新雪花的时间间隔
		 * @param snowXRange	每朵雪花在X轴左右飘动的范围
		 * @param snowXAngle	每朵雪花在X轴上下浮动的角度值
		 * @param baseYSpeed	雪花下落速度基础值
		 * @param scaleMin		雪花最小缩放值
		 * @param scaleMix		雪花最大缩放值
		 * 
		 */
		public function SnowController(parant:DisplayObjectContainer,snowShowDelay:uint=800,snowXRange:Number=2,snowXAngle:Number=0.1,baseYSpeed:Number=2.5,scaleMin:Number=0.8,scaleMix:Number=1.2)
		{
			_parant=parant;
			_snowShowDelay=snowShowDelay;
			_snowXRange=snowXRange;
			_snowXAngle=snowXAngle;
			_baseYSpeed=baseYSpeed;
			_scaleMin=scaleMin;
			_scaleMix=scaleMix;
		}
			
		public function start():void
		{
			startT();
		}
		
		private function startT():void
		{
			_timer = new Timer(_snowShowDelay);
			_timer.addEventListener(TimerEvent.TIMER,onTHandler);
			_timer.start();
		}
		
		private function onTHandler(e:TimerEvent):void
		{
			addSnow();
		}
		
		private function addSnow():void
		{
			if(!_snowA) _snowA = new Array();
			
			for(var i:int = 0; i< 2; i++)
			{
				var w:Number;
				var h:Number;
				
				if(_parant is Stage){
					w=Stage(_parant).stageWidth;
					h=Stage(_parant).stageHeight;
				}else{
					w=_parant.width;
					h=_parant.height;;
				}
				
				var s:Snow = new Snow(_snowXRange,_snowXAngle,_baseYSpeed,h+20,_scaleMin,_scaleMix);
				var num:Number = Math.random()*w/3;
				s.x = num*3;
				s.y = 20 - Math.random()*50
				_parant.addChild(s);
				_snowA.push(s);
			}
		}
		
		private function destroy():void
		{
			if(_timer)
			{
				_timer.removeEventListener(TimerEvent.TIMER,onTHandler);
				_timer.stop();
				_timer = null;
			}
			if(_snowA)
			{
				for(var i:int = 0; i< _snowA.length; i++)
				{
					var s:Snow = _snowA[i] as Snow;
					if(s)
					{
						s.destroy();
						s = null;
					}
				}
				_snowA = null;
			}
		}
	}
}


/*
用法示例：
var sc:SnowController=new SnowController(stage);
sc.start();
*/
